package com.ruoyi.samplemod.mapper;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.samplemod.domain.entity.EmilSystem;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
public interface EmailMapper {

    //通过指定部门发送邮件
    @Select("select email from sys_user where dept_id=#{dept_id} and email != \"\" ")
    List<String> getEmailBydeptId(@Param("dept_id")Long dept_id);

    //通过指定系统的用户发送邮件
    @Select("select email from sys_user INNER JOIN emil_system ON user_id=sys_user_id where sys_menu_id=#{sys_menu_id} and email is not null and email !='' and state=1")
    List<String> getEmailByMenuId(@Param("sys_menu_id")Long sys_menu_id);

    @Results(id="userEmilMap",value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "nickName", column = "nick_name")
    })
    @Select("<script>" +
            "select user_id,user_name,nick_name FROM sys_user " +
            "WHERE 1=1 " +
            "<if test='deptIdList != null and !deptIdList.isEmpty()'> " +
            "   AND dept_id IN " +
            "   <foreach item='item' collection='deptIdList' open='(' separator=',' close=')'>" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "<if test='deptIdList == null or deptIdList.isEmpty()'> " +
            "   AND 1=0 " +  // 或者根据业务需求改为其他条件
            "</if>" +
            "</script>")
    List<SysUser> getEmailBydeptIds(@Param("deptIdList") List<String> deptIdList);



    @Results(id="EmilSystemMap",value = {
            @Result(property = "sysUserId", column = "sys_user_id"),
            @Result(property = "sysMenuId", column = "sys_menu_id")
    })
    @Select("select * from emil_system where sys_menu_id=#{menu_id} and state=1")
    List<EmilSystem> querySystemUser(@Param("menu_id") Long menu_id);


    @Results(id="menuEmilMap",value = {
            @Result(property = "menuId", column = "menu_id"),
            @Result(property = "menuName", column = "menu_name")
    })
    @Select("select menu_id,menu_name from sys_menu where menu_id>=2000 and parent_id=0")
    List<SysMenu> getSystemMenu();


    @Update("update emil_system set state=0 where sys_menu_id=#{menu_id}")
    void setEmailState0(@Param("menu_id") Long menu_id);

    @Update("CREATE TEMPORARY TABLE IF NOT EXISTS temp_selected (sys_user_id BIGINT, sys_menu_id BIGINT)")
    void createTempTable();

    @Insert({
            "<script>",
            "<if test='userIdList == null or userIdList.isEmpty()'>",
            "    INSERT INTO temp_selected (sys_user_id, sys_menu_id) VALUES (NULL, NULL) -- 或跳过执行",
            "</if>",
            "<if test='userIdList != null and !userIdList.isEmpty()'>",
            "    INSERT INTO temp_selected (sys_user_id, sys_menu_id) VALUES ",
            "    <foreach collection='userIdList' item='item' separator=','>",
            "        (#{item}, #{menu_id})",
            "    </foreach>",
            "</if>",
            "</script>"
    })
    void insertSelectedParams(@Param("userIdList") List<Long> userIdList,@Param("menu_id") Long menu_id);

    @Insert("INSERT INTO emil_system (sys_user_id, sys_menu_id, state) " +
            "SELECT s.sys_user_id, s.sys_menu_id, 1 FROM temp_selected s " +
            "ON DUPLICATE KEY UPDATE state = 1")
    void upsertSelectedItems();

    @Update("DROP TEMPORARY TABLE IF EXISTS temp_selected")
    void cleanTempTable();

    @Transactional
    default void setUserEmilSystem(List<Long> userIdList,Long menu_id){
        setEmailState0(menu_id);
        createTempTable();
        insertSelectedParams(userIdList,menu_id);
        upsertSelectedItems();
        cleanTempTable();
    }
    //通过指定系统的用户发送邮件
    @Select("select email from sys_user INNER JOIN emil_system ON user_id=sys_user_id where sys_menu_id=#{sys_menu_id} and dept_id=#{dept_id} and email is not null and email !='' and state=1")
    List<String> getEmailByMenuIdByDeptId(@Param("sys_menu_id")Long sysMenuId, Long deptId);
}
